package com.douxg.fastCoding.spring.vo;

import com.douxg.fastCoding.spring.consts.BizCode;
import com.douxg.fastCoding.spring.consts.ElementMsgType;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @Description 全局统一返回结果格式
 * 此处耦合了全局业务码
 * @Auther douxg
 * @Date 2020/4/16 14:35
 */
@Data
@Accessors(chain = true)
public class Result {
    private int code;
    private String msg;
    private Object data;
    //elementui提示类型
    private String type = ElementMsgType.success.name();

    public static Result bizCode(BizCode bizCode) {
        return create(bizCode);
    }

    public static Result success() {
        return create(BizCode.SUCCESS);
    }

    public static Result success(Object data) {
        return create(BizCode.SUCCESS, data);
    }

    public static Result error() {
        return create(BizCode.OTHER);
    }

    public static Result create(BizCode bizCode) {
        return create(bizCode, null);
    }

    public static Result create(BizCode bizCode, Object data) {
        Result result = new Result().setCode(bizCode.getCode()).setMsg(bizCode.getBizMsg()).setData(data);
        if (bizCode != BizCode.SUCCESS) {
            result.setType(ElementMsgType.error.name());
        }
        return result;
    }
}
